package Bidostar_Page;

import Bidostar_BaseData.Page;
import Bidostar_Data.Baseinfo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import java.util.concurrent.TimeUnit;

public class AccountPage extends Page
{
    public AccountPage(WebDriver driver)
    {
        super(driver);
    }

    Baseinfo bi = new Baseinfo();

    public void clickaccountmanage()
    {
        getElement("accountmanage").click();
    }

    public void clickaddAccount()
    {
        getElement("addAccount").click();
    }

    public void InputAccount(String InputAccount)
    {
        getElement("InputAccount").clear();
        getElement("InputAccount").sendKeys(InputAccount);
    }

    public void InputAccountName(String InputAccountName)
    {
        getElement("InputAccountName").clear();
        getElement("InputAccountName").sendKeys(InputAccountName);
    }

    public void InputAccountTelephone(String InputAccountTelephone)
    {
        getElement("InputAccountTelephone").clear();
        getElement("InputAccountTelephone").sendKeys(InputAccountTelephone);
    }

    public void Account_Group()
    {
        getElement("Account_Group").click();
    }

    public void SaveAccount()
    {
        getElement("SaveAccount").click();
    }

    public void clickAlert_Sure()
    {
        getElement("Alert_Sure").click();
    }

    public boolean AccountTest() throws Exception
    {
        try
        {
            if (driver.getCurrentUrl().contains("dc.bidostar.com"))
            {
                logger.warn("调度平台暂无此功能");
            } else
            {
                clickaccountmanage();
                clickaddAccount();
                InputAccount(Baseinfo.Account);
                if (Baseinfo.Account.length() <= 0)
                {
                    SaveAccount();
                    logger.warn(getElement("Tip").getText());
                } else if (Baseinfo.Account.length() > 0 && Baseinfo.Account.length() <= 20)
                {
                    InputAccountName(Baseinfo.AccountName);
                    if (Baseinfo.AccountName.length() <= 0)
                    {
                        SaveAccount();
                        logger.warn(getElement("Tip").getText());
                    } else if (Baseinfo.AccountName.length() > 0 && Baseinfo.AccountName.length() <= 20)
                    {
                        InputAccountTelephone(bi.AccountTelephone());
                        if (bi.AccountTelephone().length() <= 0)
                        {
                            SaveAccount();
                            logger.warn(getElement("Tip").getText());
                        } else if (bi.AccountTelephone().length() > 0 && bi.AccountTelephone().length() < 11)
                        {
                            SaveAccount();
                            logger.warn(getElement("Tip").getText());
                        } else if (bi.AccountTelephone().length() == 11)
                        {
                            SaveAccount();
                            if (getElement("Tip").isDisplayed())
                            {
                                logger.warn(getElement("Tip").getText());
                                Account_Group();
                                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                                driver.findElement(By.xpath(".//*[@id='list_vino-pulldown']/li[1]")).click();
                                SaveAccount();
                                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                                clickAlert_Sure();
                                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                                if (driver.findElement(By.xpath(".//*[@id='app']/div[2]/table/tbody/tr[1]/td[1]")).getText().contains(Baseinfo.Account))
                                {
                                    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                                    Assert.assertEquals(driver.findElement(By.xpath(".//*[@id='app']/div[2]/table/tbody/tr[1]/td[2]")).getText(), Baseinfo.AccountName);
                                    logger.info("姓名验证通过：" + Baseinfo.AccountName);
                                    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                                    logger.info("账号创建成功，账号名称：" + Baseinfo.Account + "@test");
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }
}
